/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myfx.component;

import javafx.stage.Modality;
import javafx.stage.Stage;
import Globals.AllGlobal;
import brain.MyBrain;
import controller.MyController;
import database.operate.OperateMyDatabaseImpl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import model.GongZuoMian_Model;

/**
 *
 * @author Administrator
 */
public class GongZuoMian_Copy extends Stage{

    private Timestamp timestamp=new Timestamp(System.currentTimeMillis());
    //
    private static JFXPanel fxContainer;
    private MyController controller;
    private GongZuoMian_Model gongZuoMian_Model;
    private GongZuoMian_Model gzm_info;
    private String gongZuoMianName;
    TextFieldTreeCellImpl textFieldTreeCellImpl;
    private MyBrain brain;
    private Scene scene ;
    private Group root=new Group();
    final TextField caichangField=new TextField();
    final TextField mingchengField=new TextField(null);
    final TextField zhituField=new TextField(null);
    final TextField zhituriqiField=new TextField();
    final TextField heduiField=new TextField(null);
    final TextField kezhangField=new TextField(null);
    final TextField bianhaoField=new TextField(null);
    final TextField gongchengshiField=new TextField(null);
    final TextField gzmcdField=new TextField(null);
    final TextField gzmzxcdField=new TextField(null);
    final TextField gzmgszbXField=new TextField(null);
    final TextField gzmgszbYField=new TextField(null);
    final TextField gzmfwjField=new TextField(null);
    final TextField ahcjlField=new TextField(null);
    final TextField ychdField=new TextField(null);
    final TextField qsdxField=new TextField();
    final TextField qsdyField=new TextField();
    int gzmType=AllGlobal.DRAW_ONE;
    public GongZuoMian_Copy
            (MyController controller,String gongZuoMianName,TextFieldTreeCellImpl textFieldTreeCellImpl,MyBrain brain,
            Stage owner, boolean modality, String title)
            throws Exception{
        super();
        initOwner(owner);
        Modality m = modality ? Modality.APPLICATION_MODAL : Modality.NONE;
        initModality(m);
        setTitle(title);
        scene=new Scene(root, 500, 650, Color.WHITE);
        setScene(scene);
        
        MyCard mc=new MyCard(490, 50, "总参调节",Color.DARKGRAY);
        mc.setTranslateX(5);
        mc.setTranslateY(5);
        root.getChildren().add(mc);
        MyCard mc1=new MyCard(490, 450, "量参调节",Color.DARKGRAY);
        mc1.setTranslateX(5);
        mc1.setTranslateY(80);
        root.getChildren().add(mc1);
        MyCard mc2=new MyCard(490, 30, "输入处理",Color.DARKGRAY);
        mc2.setTranslateX(5);
        mc2.setTranslateY(555);
        root.getChildren().add(mc2);
        
        ArrayList<GongZuoMian_Model> gzmlist=OperateMyDatabaseImpl.getAllResult(AllGlobal.GONGZUOMIAN_SQL);

        if(gzmlist.size()>0){
            gzm_info=gzmlist.get(0);
            gzmType=AllGlobal.UPDATE_ONE;
            caichangField.setText(gzm_info.getCaichang());
            mingchengField.setText(gzm_info.getMingcheng());
            zhituField.setText(gzm_info.getZhitu());
            zhituriqiField.setText(gzm_info.getZhituriqi());
            heduiField.setText(gzm_info.getHedui());
            kezhangField.setText(gzm_info.getKezhang());
            bianhaoField.setText(gzm_info.getBianhao());
            gongchengshiField.setText(gzm_info.getGongchengshi());
            gzmcdField.setText(gzm_info.getGzmcd());
            gzmzxcdField.setText(gzm_info.getGzmzxcdString());
            gzmgszbXField.setText(gzm_info.getGzmgszbX());
            gzmgszbYField.setText(gzm_info.getGzmgszbY());
            gzmfwjField.setText(gzm_info.getGzmfwj());
            ahcjlField.setText(gzm_info.getAhcjl());
            final TextField ychdField=new TextField(null);
            qsdxField.setText(gzm_info.getSgqsgszbx());
            qsdyField.setText(gzm_info.getSgqsgszby());
        }else{
            gzmType=AllGlobal.DRAW_ONE;
            gzm_info=new GongZuoMian_Model();
        }
        
        this.gongZuoMianName=gongZuoMianName;
        gongZuoMian_Model=new GongZuoMian_Model();
        this.controller=controller;
        this.textFieldTreeCellImpl=textFieldTreeCellImpl;
        this.brain=brain;
        this.setResizable(false);
        createScene();

    }
    private void createScene(){
        //信息标示
        final Text warningText=new Text("当前所处状态");
        warningText.setFill(Color.GREEN);
        warningText.setFont(new Font(18));
        warningText.setTranslateX(15);
        warningText.setTranslateY(635);
        root.getChildren().add(warningText);
        //工作面类型
        Text tg_gzm_lx=new Text("工作面类型");
        ToggleGroup groupGZM = new ToggleGroup();
        final RadioButton but_gzm_left = new RadioButton("左工作面");
        final RadioButton but_gzm_right = new RadioButton("右工作面");
        but_gzm_left.setSelected(true);
        but_gzm_left.setToggleGroup(groupGZM);
        but_gzm_right.setToggleGroup(groupGZM);
        but_gzm_left.setTranslateX(100);
        but_gzm_left.setTranslateY(33);
        but_gzm_right.setTranslateY(33);
        but_gzm_right.setTranslateX(200);
        tg_gzm_lx.setTranslateY(46);
        tg_gzm_lx.setTranslateX(15);
        root.getChildren().add(tg_gzm_lx);
        root.getChildren().add(but_gzm_left);
        root.getChildren().add(but_gzm_right);
        //钻孔类型
        Text tg_zk_lx=new Text("钻孔类型");
        ToggleGroup groupZK = new ToggleGroup();
        final RadioButton but_zk_left = new RadioButton("圆柱体");
        final RadioButton but_zk_right = new RadioButton("长方体");
        but_zk_left.setSelected(true);
        but_zk_left.setToggleGroup(groupZK);
        but_zk_right.setToggleGroup(groupZK);
        but_zk_left.setTranslateX(100);
        but_zk_left.setTranslateY(53);
        but_zk_right.setTranslateY(53);
        but_zk_right.setTranslateX(200);
        tg_zk_lx.setTranslateY(66);
        tg_zk_lx.setTranslateX(15);
        root.getChildren().add(tg_zk_lx);
        root.getChildren().add(but_zk_left);
        root.getChildren().add(but_zk_right);

        Text ccText=new Text("采场名称");
        
        caichangField.setPrefSize(385, 12);
        caichangField.setTranslateX(80);
        caichangField.setTranslateY(107);
        ccText.setTranslateX(15);
        ccText.setTranslateY(122);        
        root.getChildren().add(ccText);
        root.getChildren().add(caichangField);
        Text gzmText=new Text("工作面名称");
        
        mingchengField.setPromptText("请输入工作面名称");
        mingchengField.setPrefSize(150, 12);
        mingchengField.setTranslateX(80);
        mingchengField.setTranslateY(130);
        gzmText.setTranslateX(15);
        gzmText.setTranslateY(145);
        root.getChildren().add(mingchengField);
        root.getChildren().add(gzmText);
        Text ztText=new Text("制图");
        
        zhituField.setPromptText("请输入制图工程师");
        zhituField.setPrefSize(150, 12);
        zhituField.setTranslateX(315);
        zhituField.setTranslateY(130);
        ztText.setTranslateX(250);
        ztText.setTranslateY(145);
        root.getChildren().add(zhituField);
        root.getChildren().add(ztText);
        Text ztrqText=new Text("制图日期");
        
        zhituriqiField.setEditable(false);
        zhituriqiField.setPrefSize(150, 12);
        zhituriqiField.setTranslateX(80);
        zhituriqiField.setTranslateY(153);
        ztrqText.setTranslateX(15);
        ztrqText.setTranslateY(168);
        root.getChildren().add(zhituriqiField);
        root.getChildren().add(ztrqText);
        Text hdText=new Text("核对");
        
        heduiField.setPromptText("请输入核对人员姓名，可以填无");
        heduiField.setPrefSize(150, 12);
        heduiField.setTranslateX(315);
        heduiField.setTranslateY(153);
        hdText.setTranslateX(250);
        hdText.setTranslateY(168);
        root.getChildren().add(heduiField);
        root.getChildren().add(hdText);
        Text blcText=new Text("比例尺");
        final ComboBox<String> bilichiBox=new ComboBox<String>(FXCollections.observableArrayList("1:1","1:10","1:25","1:50","1:100","1:200","1:500","1:1000","1:2000"));
        bilichiBox.setPromptText("请选择比例尺");
        bilichiBox.setTranslateX(80);
        bilichiBox.setTranslateY(176);
        blcText.setTranslateX(15);
        blcText.setTranslateY(191);
        bilichiBox.setPrefSize(150, 12);
        bilichiBox.getSelectionModel().select("1:1000");
        root.getChildren().add(blcText);
        root.getChildren().add(bilichiBox);
        Text kzText=new Text("科长");
        
        kezhangField.setPromptText("请输入科长");
        kezhangField.setPrefSize(150, 12);
        kezhangField.setTranslateX(315);
        kezhangField.setTranslateY(176);
        kzText.setTranslateX(250);
        kzText.setTranslateY(191);
        root.getChildren().add(kezhangField);
        root.getChildren().add(kzText);
        Text bhText=new Text("制图编号");
        
        bianhaoField.setPromptText("请输入制图编号");
        bianhaoField.setPrefSize(150, 12);
        bianhaoField.setTranslateX(80);
        bianhaoField.setTranslateY(199);
        bhText.setTranslateX(15);
        bhText.setTranslateY(214);
        root.getChildren().add(bianhaoField);
        root.getChildren().add(bhText);
        Text zgcsText=new Text("总工程师");
        
        gongchengshiField.setPromptText("请输入总工程师");
        gongchengshiField.setPrefSize(150, 12);
        gongchengshiField.setTranslateX(315);
        gongchengshiField.setTranslateY(199);
        zgcsText.setTranslateX(250);
        zgcsText.setTranslateY(214);
        root.getChildren().add(gongchengshiField);
        root.getChildren().add(zgcsText);
        Text gzmcdText=new Text("工作面长度");
        
        gzmcdField.setPromptText("请输入工作面长度");
        gzmcdField.setPrefSize(150, 12);
        gzmcdField.setTranslateX(80);
        gzmcdField.setTranslateY(222);
        gzmcdText.setTranslateX(15);
        gzmcdText.setTranslateY(237);
        gzmcdField.setFont(new Font(12));
        root.getChildren().add(gzmcdField);
        root.getChildren().add(gzmcdText);
        Text sccdText=new Text("顺槽长度");
        
        gzmzxcdField.setPromptText("请输入顺槽长度");
        gzmzxcdField.setPrefSize(150, 12);
        gzmzxcdField.setTranslateX(315);
        gzmzxcdField.setTranslateY(222);
        sccdText.setTranslateX(250);
        sccdText.setTranslateY(237);
        root.getChildren().add(gzmzxcdField);
        root.getChildren().add(sccdText);
        Text tishiText=new Text("注意：输入基点为工作面与回风平行的交点处！");
        tishiText.setFill(Color.RED);
        tishiText.setTranslateX(15);
        tishiText.setTranslateY(260);
        root.getChildren().add(tishiText);
        Text gszbxText=new Text("高斯坐标X");
        
        gzmgszbXField.setPromptText("请输入基点X轴高斯坐标");
        gzmgszbXField.setPrefSize(150, 12);
        gzmgszbXField.setTranslateX(80);
        gzmgszbXField.setTranslateY(268);
        gszbxText.setTranslateX(15);
        gszbxText.setTranslateY(283);
        root.getChildren().add(gzmgszbXField);
        root.getChildren().add(gszbxText);
        Text gszbyText=new Text("高斯坐标Y");
        
        gzmgszbYField.setPromptText("请输入基点Y轴高斯坐标");
        gzmgszbYField.setPrefSize(150, 12);
        gzmgszbYField.setTranslateX(315);
        gzmgszbYField.setTranslateY(268);
        gszbyText.setTranslateX(250);
        gszbyText.setTranslateY(283);
        root.getChildren().add(gzmgszbYField);
        root.getChildren().add(gszbyText);
        Text hfphfwjText=new Text("回风平巷方位角");
        
        gzmfwjField.setPromptText("请输入平巷方位角");
        gzmfwjField.setPrefSize(130, 12);
        gzmfwjField.setTranslateX(100);
        gzmfwjField.setTranslateY(291);
        hfphfwjText.setTranslateX(15);
        hfphfwjText.setTranslateY(306);
        root.getChildren().add(gzmfwjField);
        root.getChildren().add(hfphfwjText);
        Text mcxbycxxText=new Text("提示：下属为煤层下部岩层信息");
        mcxbycxxText.setFill(Color.YELLOW);
        mcxbycxxText.setTranslateX(15);
        mcxbycxxText.setTranslateY(329);
        root.getChildren().add(mcxbycxxText);
        Text ahcjlText=new Text("奥灰层距离");
        
        ahcjlField.setPromptText("请输入奥灰层与煤层的距离");
        ahcjlField.setPrefSize(150, 12);
        ahcjlField.setTranslateX(80);
        ahcjlField.setTranslateY(337);
        ahcjlText.setTranslateX(15);
        ahcjlText.setTranslateY(352);
        root.getChildren().add(ahcjlField);
        root.getChildren().add(ahcjlText);
        Text ychdText=new Text("岩层厚度");
        
        ychdField.setPromptText("请输入该岩层的厚度");
        ychdField.setPrefSize(150, 12);
        ychdField.setTranslateX(315);
        ychdField.setTranslateY(337);
        ychdText.setTranslateX(250);
        ychdText.setTranslateY(352);
        root.getChildren().add(ychdField);
        root.getChildren().add(ychdText);
        Text zbxxtsText=new Text("提示：起始点高斯坐标是坐标网格最左下角的点");
        zbxxtsText.setFill(Color.YELLOW);
        zbxxtsText.setTranslateX(15);
        zbxxtsText.setTranslateY(375);
        root.getChildren().add(zbxxtsText);
        Text qsdxText=new Text("起始点X");
        
        qsdxField.setPromptText("请输入起始点X");
        qsdxField.setTranslateX(80);
        qsdxField.setTranslateY(383);
        qsdxText.setTranslateX(15);
        qsdxText.setTranslateY(398);
        root.getChildren().add(qsdxField);
        root.getChildren().add(qsdxText);
        Text qsdyText=new Text("起始点Y");
        
        qsdyField.setPromptText("请输入起始点Y");
        qsdyField.setTranslateX(315);
        qsdyField.setTranslateY(383);
        qsdyText.setTranslateX(250);
        qsdyText.setTranslateY(398);
        root.getChildren().add(qsdyField);
        root.getChildren().add(qsdyText);
        Button cancel=new Button("取      消");
        Button ok=new Button("确      认");
        Button retry=new Button("重新输入");
        
        ok.setTranslateX(170);
        ok.setTranslateY(582);
        retry.setTranslateX(250);
        retry.setTranslateY(582);
        root.getChildren().add(ok);
        root.getChildren().add(retry);
        //添加事件
        ok.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                ArrayList<GongZuoMian_Model> gzmlist;
                try {
                    gzmlist = OperateMyDatabaseImpl.getAllResult(AllGlobal.GONGZUOMIAN_SQL);
                    if(gzmlist.size()>0){
                        gzmType=AllGlobal.UPDATE_ONE;
                    }else{
                        gzmType=AllGlobal.DRAW_ONE;
                    }
                } catch (Exception ex) {
                    Logger.getLogger(GongZuoMian_Copy.class.getName()).log(Level.SEVERE, null, ex);
                }
                //ToggleButton处理
                if(but_gzm_left.isSelected()){
                    controller.setGZM_TYPE(AllGlobal.GZM_TYPE_LEFT);
                }else{
                    controller.setGZM_TYPE(AllGlobal.GZM_TYPE_RIGHT);
                }
                if(but_zk_left.isSelected()){
                    controller.setZuankongType(AllGlobal.CYLINDER_TYPE);
                }else{
                    controller.setZuankongType(AllGlobal.CUBOID_TYPE);
                }
                String bilichiString=bilichiBox.getSelectionModel().getSelectedItem().toString();
                gongZuoMian_Model.setBilichiString(bilichiString);
                gongZuoMian_Model.setCaichang(caichangField.getText());
                gongZuoMian_Model.setMingcheng(mingchengField.getText());
                gongZuoMian_Model.setZhitu(zhituField.getText());
                gongZuoMian_Model.setZhituriqi(zhituriqiField.getText());
                gongZuoMian_Model.setHedui(heduiField.getText());
                gongZuoMian_Model.setKezhang(kezhangField.getText());
                gongZuoMian_Model.setBianhao(bianhaoField.getText());
                gongZuoMian_Model.setGongchengshi(gongchengshiField.getText());
                gongZuoMian_Model.setGzmcd(gzmcdField.getText());
                gongZuoMian_Model.setGzmzxcdString(gzmzxcdField.getText());
                gongZuoMian_Model.setGzmgszbX(gzmgszbXField.getText());
                gongZuoMian_Model.setGzmgszbY(gzmgszbYField.getText());
                gongZuoMian_Model.setGzmfwj(gzmfwjField.getText());
                gongZuoMian_Model.setAhcjl(ahcjlField.getText());
                gongZuoMian_Model.setSgqsgszbx(qsdxField.getText());
                gongZuoMian_Model.setSgqsgszby(qsdyField.getText());
                //有趣的实验
                //
                //
                //
                //
                //
//                controller.getJmeCanvas().useForTest(controller.getJmeCanvas().getTestGeo());
                if(gzmType==AllGlobal.DRAW_ONE){
                        try {
                            OperateMyDatabaseImpl.doAdd("insert into gongzuomian values ('"+
                                        gongZuoMian_Model.getMingcheng()+"','"+
                                        gongZuoMian_Model.getZhitu()+"','"+
                                        String.valueOf(System.currentTimeMillis())+"','"+
                                        gongZuoMian_Model.getHedui()+"','"+
                                        gongZuoMian_Model.getBilichiString()+"','"+
                                        gongZuoMian_Model.getKezhang()+"','"+
                                        gongZuoMian_Model.getBianhao()+"','"+
                                        gongZuoMian_Model.getGongchengshi()+"','"+
                                        gongZuoMian_Model.getCaichang()+"','"+
                                        gongZuoMian_Model.getGzmcd()+"','"+
                                        gongZuoMian_Model.getGzmzxcdString()+"','"+
                                        gongZuoMian_Model.getGzmgszbX()+"','"+
                                        gongZuoMian_Model.getGzmgszbY()+"','"+
                                        gongZuoMian_Model.getGzmfwj()+"','"+
                                        gongZuoMian_Model.getAhcjl()+"','"+
                                        gongZuoMian_Model.getSgqsgszbx()+"','"+
                                        gongZuoMian_Model.getSgqsgszby()+
                                        "');");
                            controller.setFANGWEIJIAO(Float.parseFloat(gongZuoMian_Model.getGzmfwj()));
                            warningText.setText("完成数据插入");
                        } catch (Exception ex) {
                            Logger.getLogger(GongZuoMian_Copy.class.getName()).log(Level.SEVERE, null, ex);
                            warningText.setText("数据插入失败");
                        }
                    }                    
                    else{
                        try {
                            OperateMyDatabaseImpl.doUpdate("update gongzuomian set "+
                                    "mingcheng='"+gongZuoMian_Model.getMingcheng()+"',"+
                                    "zhitu='"+gongZuoMian_Model.getZhitu()+"',"+
                                    "zhituriqi='"+gongZuoMian_Model.getZhituriqi()+"',"+
                                    "hedui='"+gongZuoMian_Model.getHedui()+"',"+
                                    "bilichi='"+gongZuoMian_Model.getBilichiString()+"',"+
                                    "kezhang='"+gongZuoMian_Model.getKezhang()+"',"+
                                    "bianhao='"+gongZuoMian_Model.getBianhao()+"',"+
                                    "gongchengshi='"+gongZuoMian_Model.getGongchengshi()+"',"+
                                    "caichang='"+gongZuoMian_Model.getCaichang()+"',"+
                                    "gzmcd='"+gongZuoMian_Model.getGzmcd()+"',"+
                                    "gzmzxcd='"+gongZuoMian_Model.getGzmzxcdString()+"',"+
                                    "gzmgszbx='"+gongZuoMian_Model.getGzmgszbX()+"',"+
                                    "gzmgszby='"+gongZuoMian_Model.getGzmgszbY()+"',"+
                                    "gzmfwj='"+gongZuoMian_Model.getGzmfwj()+"',"+ 
                                    "sgqsgszbx='"+gongZuoMian_Model.getSgqsgszbx()+"',"+ 
                                    "sgqsgszby='"+gongZuoMian_Model.getSgqsgszby()+"'"+ 
                                    " where mingcheng='"+textFieldTreeCellImpl.getTreeItem().getValue()+"';");
                            controller.setFANGWEIJIAO(Float.parseFloat(gongZuoMian_Model.getGzmfwj()));
                            warningText.setText("完成了数据的修改");
                        } catch (Exception ex) {
                            Logger.getLogger(GongZuoMian_Copy.class.getName()).log(Level.SEVERE, null, ex);
                            warningText.setText("数据修改失败");
                        }
                    }
                    textFieldTreeCellImpl.getTreeItem().setValue(mingchengField.getText());
            }
        });
        retry.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                caichangField.setText(gzm_info.getCaichang());
                mingchengField.setText(gzm_info.getMingcheng());
                zhituField.setText(gzm_info.getZhitu());
                zhituriqiField.setText(gzm_info.getZhituriqi());
                heduiField.setText(gzm_info.getHedui());
                kezhangField.setText(gzm_info.getKezhang());
                bianhaoField.setText(gzm_info.getBianhao());
                gongchengshiField.setText(gzm_info.getGongchengshi());
                gzmcdField.setText(gzm_info.getGzmcd());
                gzmzxcdField.setText(gzm_info.getGzmzxcdString());
                gzmgszbXField.setText(gzm_info.getGzmgszbX());
                gzmgszbYField.setText(gzm_info.getGzmgszbY());
                gzmfwjField.setText(gzm_info.getGzmfwj());
                ahcjlField.setText(gzm_info.getAhcjl());
                final TextField ychdField=new TextField(null);
                qsdxField.setText(gzm_info.getSgqsgszbx());
                qsdyField.setText(gzm_info.getSgqsgszby());
                warningText.setText("重新输入数据");
            }
        });
    }
}

